package com.tiny.persistence.repository.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.tiny.persistence.domain.WxMenu;
import com.tiny.persistence.repository.plus.BgMenuRepoPlus;
import com.tiny.persistence.repository.plus.WxMenuRepoPlus;
/**
 *后台菜单仓储层
* @ClassName:  
* @Description: TODO 
* @author:  
* @date: 2014年3月23日
*
 */
public class WxMenuRepoImpl extends BaseRepoImpl<WxMenu> implements WxMenuRepoPlus{

    private static Logger logger = Logger.getLogger(WxMenuRepoImpl.class);

    @Override
    public List<WxMenu> getListByCondition(Map<String, Object> condition) {
        Query query = em.createQuery("from WxMenuRepoImpl as re");
        
        //query.setFirstResult(arg0);
        //query.setMaxResults(arg0);
        
        @SuppressWarnings("unchecked")
		List<WxMenu> list = query.getResultList();
        
        logger.info("======= " + list);
        
        return list;
    }

    @Override
    public int getCountByCondition(Map<String, Object> condition) {
        
        Query query = em.createQuery("select count(*) from WxMenu as au");
        Object obj = query.getSingleResult();
        
        logger.info("======= count=" + obj);
        
        return Integer.parseInt(obj.toString());
    }

    public List<WxMenu> findFirstLevelMenuList(int groupId) {
    	String hql = "from WxMenu o where o.parentId = -1 and o.id in(select bgMenuId from BgroleMenu where bgRoleId = "+groupId+")";
        Query query = em.createQuery(hql);
        
        @SuppressWarnings("unchecked")
		List<WxMenu> list = query.getResultList();
        
        logger.info("======= " + list);
        
        return list;
    }

    public List<WxMenu> findSecondLevelMenuListByMenuId(int menuId) {
        
        Query query = em.createQuery("from WxMenu o where o.parentId = "+menuId);
        
        @SuppressWarnings("unchecked")
		List<WxMenu> list = query.getResultList();
        
        logger.info("======= " + list);
        
        return list;
    }

	public List<WxMenu> findAllMenuList(int wxRoleId) {
		String hql = "from WxMenu o  where o.id in(select bgMenuId from BgRoleMenu where wxRoleId = "+wxRoleId+")";
        Query query = em.createQuery(hql);
        
        @SuppressWarnings("unchecked")
		List<WxMenu> list = query.getResultList();
        
        logger.info("======= " + list);
        
        return list;
	}

	public List<WxMenu> findAllMenuList() {
		Query query = em.createQuery("from WxMenu o ");

		@SuppressWarnings("unchecked")
		List<WxMenu> list = query.getResultList();

		logger.info("======= " + list);

		return list;
	}

}
